// Copyright (c) Martin Ridgers
// License: http://opensource.org/licenses/MIT

#pragma once

#include <conio.h>
#include <io.h>
#include <limits.h>
#include <process.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <wchar.h>

#include "hooks.h"

// here be dragons (for purposes of utf-8 and capturing stdio)
//
//
int     hooked_fwrite(const void*, int, int, FILE*);
void    hooked_fprintf(FILE*, const char*, ...);
int     hooked_putc(int, FILE*);
void    hooked_fflush(FILE*);
int     hooked_fileno(FILE*);
size_t  hooked_mbrtowc(wchar_t*, const char*, size_t, mbstate_t*);
size_t  hooked_mbrlen(const char*, size_t, mbstate_t*);
int     hooked_stat(const char*, struct hooked_stat*);
int     hooked_fstat(int, struct hooked_stat*);
int     wcwidth(int);

#if defined(__MINGW32__)
#   undef fwrite
#   undef fprintf
#   undef putc
#   undef fflush
#   undef fileno
#   undef mbrtowc
#   undef mbrlen
#   undef stat
#   undef fstat
#   define __MSDOS__

    // We compile Readline the same way MSVC does for consistency.
#   if defined(BUILD_READLINE)
#       undef __MINGW32__
#   endif
#endif

#if defined(BUILD_READLINE)
#   define fwrite           hooked_fwrite
#   define fprintf          hooked_fprintf
#   define putc             hooked_putc
#   define fflush           hooked_fflush
#   define fileno           hooked_fileno
#   define mbrtowc          hooked_mbrtowc
#   define mbrlen           hooked_mbrlen
#   define stat             hooked_stat
#   define fstat            hooked_fstat
#endif // BUILD_READLINE

#undef MB_CUR_MAX
#define MB_CUR_MAX  4       // 4-bytes is enough for the Unicode standard.

// msvc vs posix|readline|gnu
//
#if defined(_MSC_VER)
#   define strncasecmp      strnicmp
#   define strcasecmp       _stricmp
#   define strchr           strchr
#   define getpid           _getpid
#   define snprintf         _snprintf

#   define __STDC__         0
#   define __MSDOS__
/*
#   define __MINGW32__
#   define __WIN32__
*/

#   pragma warning(disable : 4018)  // signed/unsigned mismatch
#   pragma warning(disable : 4090)  // different 'const' qualifiers
#   pragma warning(disable : 4101)  // unreferenced local variable
#   pragma warning(disable : 4244)  // conversion from 'X' to 'Y', possible loss of data
#   pragma warning(disable : 4267)  // conversion from 'X' to 'Y', possible loss of data
#endif // _MSC_VER

#define RL_LIBRARY_VERSION  "6.2"

// What follows was generated by autotools...
//
#define RETSIGTYPE void
#define VOID_SIGHANDLER 1
#define PROTOTYPES 1
#define HAVE_ISASCII 1
#define HAVE_ISWCTYPE 1
#define HAVE_ISWLOWER 1
#define HAVE_ISWUPPER 1
#define HAVE_ISXDIGIT 1
#define HAVE_MBRLEN 1
#define HAVE_MBRTOWC 1
#define HAVE_MBSRTOWCS 1
#define HAVE_MEMMOVE 1
//#define HAVE_PUTENV 1
#define HAVE_SETLOCALE 1
#define HAVE_STRCASECMP 1
#define HAVE_STRCOLL 1
#define STRCOLL_BROKEN 1
#define HAVE_STRPBRK 1
#define HAVE_TOWLOWER 1
#define HAVE_TOWUPPER 1
#define HAVE_VSNPRINTF 1
#define HAVE_WCRTOMB 1
#define HAVE_WCSCOLL 1
#define HAVE_WCTYPE 1
#define HAVE_WCWIDTH 1
#define STDC_HEADERS 1
#define HAVE_DIRENT_H 1
#define HAVE_FCNTL_H 1
#define HAVE_LIMITS_H 1
#define HAVE_LOCALE_H 1
#define HAVE_MEMORY_H 1
#define HAVE_STDARG_H 1
#define HAVE_STDLIB_H 1
#define HAVE_STRING_H 1
#define HAVE_WCHAR_H 1
#define HAVE_WCTYPE_H 1
#define HAVE_MBSTATE_T 1
#define HAVE_WCHAR_T 1
#define HAVE_WCTYPE_T 1
#define HAVE_WINT_T 1
#define VOID_SIGHANDLER 1
#define MUST_REINSTALL_SIGHANDLERS 1
#if !defined (HAVE_TERMIOS_H) || !defined (HAVE_TCGETATTR) || defined (ultrix)
#  define TERMIOS_MISSING
#endif

#if defined (__STDC__) && defined (HAVE_STDARG_H)
#  define PREFER_STDARG
#  define USE_VARARGS
#else
#  if defined (HAVE_VARARGS_H)
#    define PREFER_VARARGS
#    define USE_VARARGS
#  endif
#endif
